#include <mathematics/Polygon.h>

namespace ne
{
    Polygon::Polygon()
    {
    }

    Polygon::Polygon(const Polygon &p)
    {
        size_t n = p.getNumVertex();
        for (size_t i=0; i<n; ++i)
        {
            mVertexList.push_back(p[i]);
        }
    }

    Polygon& Polygon::operator = (const Polygon &p)
    {
        mVertexList.clear();
        size_t n = p.getNumVertex();
        for (size_t i=0; i<n; ++i)
        {
            mVertexList.push_back(p[i]);
        }
        return *this;
    }

    Vector2& Polygon::operator [] (const size_t v)
    {
        assert(v < mVertexList.size());
        return mVertexList[v];
    }

    const Vector2& Polygon::operator [] (const size_t v) const
    {
        assert(v < mVertexList.size());
        return mVertexList[v];
    }

    real* Polygon::ptr()
    {
        if (mVertexList.empty())
        {
            return 0;
        }
        return mVertexList[0].ptr();
    }

    const real* Polygon::ptr() const
    {
        if (mVertexList.empty())
        {
            return 0;
        }
        return mVertexList[0].ptr();
    }

    void Polygon::insertVertex(const size_t index, const Vector2 &v)
    {
        if (index >= mVertexList.size())
        {
            mVertexList.push_back(v);
        }
        std::vector<Vector2>::iterator pos = mVertexList.begin();
        pos += index;
        mVertexList.insert(pos, v);
    }

    void Polygon::pushBackVertex(const Vector2 &v)
    {
        mVertexList.push_back(v);
    }

    bool Polygon::popBackVertex()
    {
        if (mVertexList.empty())
        {
            return false;
        }
        mVertexList.pop_back();
        return true;
    }

    bool Polygon::eraseVertex(const size_t index)
    {
        if (index >= mVertexList.size())
        {
            return false;
        }
        std::vector<Vector2>::iterator pos = mVertexList.begin();
        pos += index;
        mVertexList.erase(pos);
        return true;
    }

    void Polygon::clearAllVertex()
    {
        mVertexList.clear();
    }

    size_t Polygon::getNumVertex() const
    {
        return mVertexList.size();
    }

    GeometryClass Polygon::getClass() const
    {
        return GC_POLYGON;
    }

    Polygon::~Polygon()
    {
        clearAllVertex();
    }
}
